package cn.hfatec.shms.hr.hrContract.service;

import java.util.List;
import java.util.HashMap;


import cn.hfatec.shms.hr.hrEmployee.model.HrEmployee;
import cn.hfatec.shms.hr.hrEmployee.service.HrEmployeeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hfatec.base.BaseDao;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.shms.hr.hrContract.model.HrContract;
import cn.hfatec.shms.hr.hrContract.dao.HrContractDao;
import cn.hfatec.shms.hr.hrContract.model.HrContractParams;

import javax.annotation.Resource;



/**
 *
 * @author hfatec
 * @version 1.0
 *
 * the JAVA code is generate by middlegen
 *
 */
@Service("HrContractService")
public class HrContractService {

    public static final String BEAN_ID = "HrContractService";

    //注入Dao层
    @Resource(name = "HrContractDao")
    private HrContractDao hrContractDao;

    //注入BaseDao
    @Resource(name = "BaseDao")
    private BaseDao baseDao;
    //服务层
    @Resource(name = "HrEmployeeService")
    private HrEmployeeService hrEmployeeService;

    /**
     *
     * 查询数据
     *
     * @param hrContractParams
     * @param page
     *
     */
    @Transactional
    public Table queryHrContractTable(HrContractParams hrContractParams, Page page){

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("params", hrContractParams);
        return baseDao.queryTableByPage("queryHrContract", params, page);
    }

    /**
     *
     * 插入数据
     * @param hrContract
     *
     */
    @Transactional
    public void saveHrContract(HrContract hrContract) {
        HrEmployee hrEmployee=hrEmployeeService.getHrEmployee(hrContract.getEmployeeId());
        if(hrContract.getId() != null) {
            HrContract result = getHrContract(hrContract.getId());
            if(result != null) {
                hrContractDao.update(hrContract);
            } else {
                hrContractDao.insert(hrContract);
            }
        } else {
            hrContractDao.insert(hrContract);
        }
        if(hrEmployee!=null)
        {
            HrContract   hrContractSelect  =getHrContract(hrContract.getEmployeeId());
            hrEmployee.setContractStart(hrContractSelect.getContractStart());
            hrEmployee.setContractEnd(hrContractSelect.getContractEnd());
            hrEmployeeService.saveHrEmployee(hrEmployee);

        }
    }

    /**
     *
     * 删除数据
     * @param id
     *
     */
    @Transactional
    public void deleteHrContract(Integer id) {

        hrContractDao.delete(id);
    }

    /**
     *
     * 删除数据
     * @param hrContractList
     *
     */
    @Transactional
    public void deleteHrContractBatch(List<String> hrContractList) {
        for(String id : hrContractList) {
            deleteHrContract(Integer.valueOf(id));
        }
    }

    /**
     *
     * 根据ID获取数据
     * @param id
     *
     */
    @Transactional
    public HrContract getHrContract(Integer id) {

        return hrContractDao.getHrContractById(id);
    }

    /**
     *
     * 根据ID获取数据
     * @param employeeId
     *
     */
    @Transactional
    public HrContract getHrContract(String employeeId) {

        return hrContractDao.getByContractId(employeeId);
    }
}